package edu.pitt.dbmi.odie.gapp.gwt.client.filter;

import java.util.ArrayList;

import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.ColorPickerItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.DataArrivedEvent;
import com.smartgwt.client.widgets.grid.events.DataArrivedHandler;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

import edu.pitt.dbmi.odie.gapp.gwt.client.navigator.ODIE_NavigatorPanel;
import edu.pitt.dbmi.odie.gapp.gwt.client.util.panel.ODIE_ButtonItemPanel;

public class ODIE_FilterPanel extends VLayout {
	
	private VLayout clsLayout = null;

	private ListGrid clsGrid = null;

	private DynamicForm clsForm = null;

	public ODIE_FilterPanel() {
		setWidth100() ;
		refresh();
	}

	public void refresh() {
		if (clsLayout != null) {
			removeMember(clsLayout) ;
			clsLayout = null ;
		}
		clsLayout = buildClsLayout();
		addMember(clsLayout);
		draw();
	}

	private VLayout buildClsLayout() {
		VLayout clsLayout = new VLayout();
		clsLayout.setWidth100() ;
		clsLayout.setMembersMargin(10);
		
		buildClsForm();
		buildClsGrid();
		Canvas actionPanel = buildActionPanel() ;
		
//		IButton button = new IButton("Save");
//		button.addClickHandler(new ClickHandler() {
//			public void onClick(ClickEvent event) {
//				saveClsFormData();
//			}
//		});
		clsLayout.addMember(clsGrid);
		clsLayout.addMember(clsForm);
		clsLayout.addMember(actionPanel);
		
		return clsLayout;
	}
	
	private Canvas buildActionPanel() {
		
		final ButtonItem saveButtonItem = ODIE_ButtonItemPanel.buildButtonItem("Save") ;
		saveButtonItem.setStartRow(true) ;
		saveButtonItem.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				saveClsFormData();
			}
		});
		
		final ButtonItem clearButtonItem = ODIE_ButtonItemPanel.buildButtonItem("Clear") ;
		clearButtonItem.setEndRow(true) ;
		clearButtonItem.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				clearClsFormData() ;
			}
		});
		
		ArrayList<ButtonItem> buttons = new ArrayList<ButtonItem>() ;
		buttons.add(saveButtonItem) ;
		buttons.add(clearButtonItem) ;
		
		return new ODIE_ButtonItemPanel("Actions", buttons) ;

	}

	private void buildClsGrid() {

		clsGrid = new ListGrid() {
			protected String getCellCSSText(ListGridRecord record, int rowNum,
					int colNum) {
				String result = super.getCellCSSText(record, rowNum, colNum);
				if (colNum == 2) {
					String htmlColor = record.getAttribute("color");
					result = "{color:" + htmlColor
					+ ";}";
//					result = "{color:#000000; background-color:" + htmlColor
//							+ ";}";
				}
				return result;
			}

			protected String getBaseStyle(ListGridRecord record, int rowNum,
					int colNum) {
				String result = super.getBaseStyle(record, rowNum, colNum);
//				if (colNum == 2) {
//					result = "odieColorPickerBaseCell";
//				}
				return result;
			}
		};
		
		clsGrid.setWidth100();
		clsGrid.setHeight("40%");
		clsGrid.setShowAllRecords(true);
		clsGrid.setAutoFetchData(false);
		clsGrid.setAlternateRecordStyles(true) ;
		clsGrid.setAlternateRecordStyles(true);
		clsGrid.setSelectionType(SelectionStyle.SINGLE);
		clsGrid.setCanSort(true) ;
		clsGrid.setCanAcceptDrop(false) ;
		clsGrid.setCanEdit(false) ;
		clsGrid.setCanReorderRecords(false) ;
		clsGrid.setCanFreezeFields(false) ;
		clsGrid.setCanReorderFields(false) ;
		clsGrid.setCanGroupBy(false) ;
		clsGrid.setCanHover(true);
		clsGrid.setCanSelectAll(false);
		clsGrid.setCanSelectText(false) ;
		clsGrid.setCanResizeFields(false) ;
		clsGrid.setShowHeaderContextMenu(false) ;
		clsGrid.setHoverWidth(300) ;
		clsGrid.addRecordClickHandler(new RecordClickHandler() {
			public void onRecordClick(RecordClickEvent event) {
				clsForm.reset();
				clsForm.editSelectedData(clsGrid);
			}
		});
		clsGrid.addDataArrivedHandler(new DataArrivedHandler() {
			public void onDataArrived(DataArrivedEvent event) {
				clsGrid.setSortField("clsName") ;
				clsGrid.sort() ;
				clsGrid.setCanSort(false) ;
				clsGrid.redraw();
			}}) ;
		clsGrid.setDataSource(ODIE_ClsDS.getInstance());
		clsGrid.fetchData();
		clsGrid.draw();
	}

	private void buildClsForm() {

		clsForm = new DynamicForm();

		clsForm.setWidth100() ;
		clsForm.setIsGroup(true);
		clsForm.setGroupTitle("Update");
		clsForm.setNumCols(4);
		clsForm.setHeight("50%");

		TextItem clsNameItem = new TextItem();
		clsNameItem.setTitle("Cls Name");
		clsNameItem.setName("clsName") ;
//		clsNameItem.setHint("<nobr>Preferred Name of the Cls</nobr>");

		TextItem ontNameItem = new TextItem();
		ontNameItem.setTitle("Ont Code");
		ontNameItem.setName("ontologyName") ;
//		ontNameItem.setHint("<nobr>Bioportal code for ontology source</nobr>");

		TextItem cuiItem = new TextItem();
		cuiItem.setTitle("Cui");
		cuiItem.setName("cui") ;
//		cuiItem.setHint("<nobr>A plain text field</nobr>");

		ColorPickerItem colorItem = new ColorPickerItem();
		colorItem.setTitle("Color");
		colorItem.setName("color") ;
		colorItem.setWidth(85);
//		colorItem.setHint("<nobr>Color of cls in Navigator</nobr>");
		
		clsForm.setDataSource(ODIE_ClsDS.getInstance());
		
		clsForm.setFields(clsNameItem, ontNameItem, cuiItem, colorItem) ;

	}

	private void saveClsFormData() {
		clsForm.saveData();
		clearClsFormData() ;
		if (ODIE_NavigatorPanel.self != null) {
			ODIE_NavigatorPanel.self.setCurrentAnalysis(null) ;
		}
	}
	
	private void clearClsFormData() {
		clsGrid.deselectAllRecords() ;
		clsForm.clearValues() ;
	}

}
